package com.atguigu.gmall.item.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.item.config.ThreadPoolExecutorConfig;
import com.atguigu.gmall.item.service.ItemService;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    private ListFeignClient listFeignClient;

    //数据汇总
    @Override
    public Map<String, Object> getItem(Long skuId) {
        //声明一个集合对象
        Map<String, Object> map = new HashMap<>();
        //使用异步编排
        //获取skuInfo数据
        CompletableFuture<SkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            //获取skuInfo数据
            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            map.put("skuInfo", skuInfo);
            return skuInfo;
        },threadPoolExecutor);

        //获取商品价格
        CompletableFuture<Void> priceCompletableFuture = CompletableFuture.runAsync(() -> {
            BigDecimal price = productFeignClient.getSkuPrice(skuId);
            map.put("price", price);
        },threadPoolExecutor);

        //根据三级分类id获取分类数据
        CompletableFuture<Void> categoryViewCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            BaseCategoryView categroyView = productFeignClient.getCategroyView(skuInfo.getCategory3Id());
            map.put("categoryView", categroyView);
        },threadPoolExecutor);

        //获取商品的规格与包装数据 本质就是获取商品的平台属性与属性值
        CompletableFuture<Void> AttrCompletableFuture = CompletableFuture.runAsync(() -> {
            List<BaseAttrInfo> attrList = productFeignClient.getAttrList(skuId);
            //拉姆达表达式
            List<Map<String, Object>> skuAttrList = attrList.stream().map(baseAttrInfo -> {
                //创建一个map
                Map<String, Object> attrMap = new HashMap<>();
                attrMap.put("attrName", baseAttrInfo.getAttrName());
                attrMap.put("attrValue", baseAttrInfo.getAttrValueList().get(0).getValueName());
                return attrMap;
            }).collect(Collectors.toList());
            //规格：需要获取的是 AttrName  AttrValue
            map.put("skuAttrList", skuAttrList);
        },threadPoolExecutor);


        //获取销售属性+销售属性值+锁定
        CompletableFuture<Void> spuSaleAttrListCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            List<SpuSaleAttr> spuSaleAttrList = productFeignClient.getSpuSaleAttrListCheckBySku(skuInfo.getSpuId(), skuId);
            map.put("spuSaleAttrList", spuSaleAttrList);
        }, threadPoolExecutor);


        //实现商品切换数据功能
        CompletableFuture<Void> idsMapCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            Map idsMap = productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
            String valuesSkuJson = JSON.toJSONString(idsMap);//{"3716|3718":"18","3717,3718":"17"}
            map.put("valuesSkuJson", valuesSkuJson);
        },threadPoolExecutor);

        //根据spuId获取海报集合
        CompletableFuture<Void> spuPosterListCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            List<SpuPoster> spuPosterList = productFeignClient.findSpuPosterBySpuId(skuInfo.getSpuId());
            map.put("spuPosterList", spuPosterList);
        }, threadPoolExecutor);

        //使用多线程调用热度排名
        CompletableFuture<Void> hotScoreCompletableFuture = CompletableFuture.runAsync(() -> {
            listFeignClient.incrHotScore(skuId);
        }, threadPoolExecutor);

        //需要将所有的异步编排线程进行组合
        CompletableFuture.allOf(
                skuInfoCompletableFuture,
                priceCompletableFuture,
                categoryViewCompletableFuture,
                AttrCompletableFuture,
                spuSaleAttrListCompletableFuture,
                idsMapCompletableFuture,
                spuPosterListCompletableFuture,
                hotScoreCompletableFuture
        ).join();

        return map;
    }
}
